<?php


namespace App\Http\Traits;


use Illuminate\Support\Arr;

class GlobalConstant
{

    //统一数组分隔符
    const DELIMITER_CHARACTER = ",";

    const IMAGE_TYPE = [
        'jpg'  => 'jpg',
        'jpeg' => 'jpeg',
        'png'  => 'png',
        'bmp'  => 'bmp',
        'webp' => 'webp'
    ];

    const VIDEO_TYPE = [
        'mp4' => 'mp4',
        'flv' => 'flv',
        'avi' => 'avi',
        'mov' => 'mov'
    ];

    const USER_ROLE_LIST = [
        1 => '导师',
        2 => '学生',
    ];

    const TEAM_ROLE      = [
        0 => '',
        1 => '队长'
    ];
    const PROJECT_STATUS = [
        1 => '进行中',
        2 => '已结束'
    ];

    const SURVEY_TYPE = [
        'text',
        'time',
        'pictures',
        'audios',
        'videos'
    ];

    public static function getUserRoleMap($key = "ALL")
    {
        $ret = self::USER_ROLE_LIST;
        if ($key === "ALL") {
            return $ret;
        }
        return Arr::get($ret, $key, false);
    }


    public static function getTeamRoleMap($key = "ALL")
    {
        $ret = self::TEAM_ROLE;
        if ($key === "ALL") {
            return $ret;
        }
        return Arr::get($ret, $key, false);
    }

    public static function changeCoords($array, $is_string = false)
    {
        if (!$is_string) {
            $return = [];
            $coords = explode(',', $array);
            for ($i = 1; $i <= count($coords); $i++) {
                $arr[] = $coords[$i - 1];
                if ($i % 2 == 0) {
                    $return[] = $arr;
                    unset($arr);
                }
            }
            return json_encode($return);
        }
        $temp = json_decode($array, true);
        $str  = '';
        foreach ($temp as $item) {
            $str .= implode(',', $item);
            $str .= ',';
        }
        return substr($str, 0, -1);
    }

    public static function formatCoords($data)
    {
        $array = [];
        $temp  = json_decode($data, true);
        foreach ($temp as $item) {
            $arr = [];
            foreach ($item as $value) {
                $arr[] = floatval($value);
            }
            $array[] = $arr;
        }
        return $array;
    }

    public static function formatAppCoords($data)
    {
        $array = [];
        $temp  = json_decode($data, true);
        foreach ($temp as $item) {
            $arr            = new \stdClass();
            $arr->lat  = floatval($item[1]);
            $arr->lng = floatval($item[0]);
            $array[]        = $arr;
            unset($arr);
        }
        return $array;
    }

}
